package com.linkoog.devtools.icon_viewer;

import com.intellij.openapi.vfs.VirtualFile;
import com.linkoog.devtools.icon_viewer.core.Base64;
import com.linkoog.devtools.icon_viewer.core.IconType;
import com.linkoog.devtools.icon_viewer.core.ImageWrapper;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Collection;

public interface ImageToIconConverter {

    Collection<String> getExtensions();

    IconType getIconType();

    /** load an image from a byte stream */
    Image loadImage(ByteArrayInputStream byteArrayInputStream, VirtualFile virtualFile) throws IOException;

    /**
     * Convert a virtual file into an icon
     *
     * @param canonicalFile the file containing the image
     * @param canonicalPath the path of the image
     * @return an icon if convertible
     * @throws IOException if error
     */
    Icon convert(VirtualFile canonicalFile, String canonicalPath) throws IOException;

    /** Convert the image of an image wrapper in base64. */
    String toBase64(ImageWrapper imageWrapper);


    /** Wrap an image as an ImageWrapper. */
    default ImageWrapper getImageWrapper(VirtualFile virtualFile){
        Image image = null;
        byte[] fileContents = null;
        try {
            fileContents = virtualFile.contentsToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileContents);
            image = loadImage(byteArrayInputStream, virtualFile);

            if (image != null){
                return new ImageWrapper(getIconType(), image, fileContents);
            }
        } catch (IOException ex) {

        }
        return null;
    }

    /** Load an image as base64. */
    default ImageWrapper fromBase64(String base64, IconType  iconType, VirtualFile canonicalFile){
        byte[] decodedBase64 = Base64.decode(base64);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodedBase64);

        Image image = null;
        try {
            image = loadImage(byteArrayInputStream, canonicalFile);
            if (image != null){
                return new ImageWrapper(iconType, image, decodedBase64);
            }
        } catch (IOException ex) {
        }
        return null;
    }

    /** Checks if filename is accepted by this converter. */
    default Boolean isAccepted(String fileName){
        String fileExt = getExtension(fileName);
        final Collection<String> extensions = getExtensions();
        if (extensions == null || extensions.isEmpty()){
            return false;
        }

        for (String extension : extensions) {
            if (extension.equalsIgnoreCase(fileExt)){
                return true;
            }
        }

        return false;
    }

    public static String getExtension(String fileName){
        if (StringUtils.isBlank(fileName)){
            return null;
        }

        int index = fileName.lastIndexOf(".");
        if (index == -1){
            return null;
        }

        return fileName.substring(index + 1);
    }
}
